﻿///<reference path="_references.ts" />

module ScrambledEggs {    
    export interface IGameState {
        Version: number;
        Phase: GamePhase;
        Gender: Gender;
        Name: string;
        Message: string;

        PhysicalMaturity: number;
        MentalMaturity: number;
        Fullness: number;
        LeftPen: bool;

        Popularity: number;
        Intelligence: number;
        Time: number;
        Energy: number;
        Money: number;

        JobCooldown: number;
        JobLevel: number;

        DecisionCatFood: CatFoodDecision;
        DecisionBabyFinale: BabyFinaleDecision;
        Hat: string;
        PartTimeJob: PartTimeJob;

        HintsOn: bool;
        Cooldowns: { [selector: string]: number; };
        Inventory: { [item: string]: number; };
        Temporary: { [name: string]: any; };
    }
    export interface ISettings {
        IgnoreEarlyEnding: bool;
    }
    export enum PartTimeJob {
        None,
        Manager,
        Cleaning,
        Shoemaker,
        Banker,
        Research
    }
    export enum CatFoodDecision {
        None,
        Reached,
        Ate,
        Threw,
    }
    export enum BabyFinaleDecision {
        None,
        Poopcat,
        Doorbang,
        Petcat,
        Jailbreak,
    }
    export enum GamePhase {
        Fetus,
        Baby,
        YoungAdult
    }
    export enum Gender {
        Unknown,
        Male,
        Female,
    }

    export class GameStateProvider {
        public State: IGameState;
        public Settings: ISettings;
        public Key: string;

        public static DefaultState: IGameState = {
            Phase: GamePhase.Fetus,
            Gender: Gender.Unknown,
            Message: null,
            PhysicalMaturity: -1,
            MentalMaturity: -1,
            Popularity: -1,
            Intelligence: -1,
            Time: -1,
            Energy: -1,
            Money: -1,
            Name: null,
            Fullness: -1,
            JobCooldown: 0,
            LeftPen: false,
            DecisionCatFood: CatFoodDecision.None,
            DecisionBabyFinale: BabyFinaleDecision.None,
            Hat: "",
            PartTimeJob: PartTimeJob.None,
            JobLevel: 0,
            Cooldowns: {},
            HintsOn: true,
            Temporary: {},
            Inventory: {},
            Version: 14,
        }

        constructor(key: string) {
            this.Key = key;
            this.Settings = $("#settings").data("settings");
        }
        
        load(callback: () => void ): void {
            var me = this;
            $.ajax({
                url: "/Api/Saves",
                method: "GET",
                data: { key: this.Key }
            }).done(function (data) {
                me.State = <IGameState>JSON.parse(data);
                console.log("state loaded");
                console.log(me.State);
                me.upgrade();
                callback();
            }).fail(function () {
                me.State = <IGameState>$.extend(GameStateProvider.DefaultState, {});
                console.log("loaded default state");
                console.log(me.State);
                callback();
            });
        }

        save(callback: () => void ): void {
            var me = this;
            $.ajax({
                url: "/Api/Saves",
                method: "PUT",
                data: { key: me.Key, state: JSON.stringify(me.State) }
            }).done(function () {
                console.log("state saved to ?key=" + me.Key);
                console.log(me.State);
                callback();
            });
        }

        upgrade() {
            if (this.State.Version == GameStateProvider.DefaultState.Version) {
                console.log("loaded version is up to date");
                return;
            }
            switch (this.State.Version) {
                case null:
                case 0:
                    throw "Could not upgrade from unknown version";
                case 1:
                    console.log("upgrading version from 1 to 2");
                    this.State.Message = null;
                    console.log(this.State);
                case 2:
                    console.log("upgrading version from 2 to 3");
                    (<any>this.State).Physique = (<any>GameStateProvider.DefaultState).Physique;
                    (<any>this.State).Intelligence = (<any>GameStateProvider.DefaultState).Intelligence;
                    console.log(this.State);
                case 3:
                    console.log("upgrading version from 3 to 4");
                    this.State.Name = prompt("Missing data: Name; Please update", "King Flar");;
                    console.log(this.State);
                case 4:
                    console.log("upgrading version from 4 to 5");
                    this.State.Fullness = GameStateProvider.DefaultState.Fullness;
                    console.log(this.State);
                case 5:
                    console.log("upgrading version from 5 to 6");
                    this.State.Cooldowns = {};
                    this.State.Temporary = {};
                    console.log(this.State);
                case 6:
                    console.log("upgrading version from 6 to 7");
                    this.State.Inventory = {};
                    this.State.LeftPen = false;
                    console.log(this.State);
                case 7:
                    console.log("upgrading version from 7 to 8");
                    (<any>this.State).CatFood = 0;
                    console.log(this.State);
                case 8:
                    console.log("upgrading version from 8 to 9");
                    this.State.DecisionCatFood = (<any>this.State).CatFood;
                    this.State.DecisionBabyFinale = BabyFinaleDecision.None;
                    console.log(this.State);
                case 9:
                    console.log("upgrading version from 9 to 10");
                    this.State.MentalMaturity = (<any>this.State).Physique;
                    this.State.PhysicalMaturity = (<any>this.State).Intelligence;
                    delete (<any>this.State).Physique;
                    delete (<any>this.State).Intelligence;
                    this.State.Popularity = GameStateProvider.DefaultState.Popularity;
                    this.State.Intelligence = GameStateProvider.DefaultState.Intelligence;
                    this.State.Time = GameStateProvider.DefaultState.Time;
                    this.State.Energy = GameStateProvider.DefaultState.Energy;
                    this.State.Money = GameStateProvider.DefaultState.Money;
                    console.log(this.State);
                case 10:
                    console.log("upgrading version from 10 to 11");
                    this.State.PartTimeJob = GameStateProvider.DefaultState.PartTimeJob;
                    console.log(this.State);
                case 11:
                    console.log("upgrading version from 11 to 12");
                    this.State.JobCooldown = GameStateProvider.DefaultState.JobCooldown;
                    console.log(this.State);
                case 12:
                    console.log("upgrading version from 12 to 13");
                    this.State.HintsOn = GameStateProvider.DefaultState.HintsOn;
                    console.log(this.State);
                case 13:
                    console.log("upgrading version from 13 to 14");
                    this.State.Hat = GameStateProvider.DefaultState.Hat;
                    console.log(this.State);
                case 14:
                    console.log("upgrading version from 14 to 15");
                    this.State.JobLevel = GameStateProvider.DefaultState.JobLevel;
                    console.log(this.State);
            }
            this.State.Version = GameStateProvider.DefaultState.Version;
        }
    }
}